add_custom_command(OUTPUT documented_examples
	COMMAND ${CMAKE_SOURCE_DIR}/examples/generate_documented.sh
	WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/examples
	COMMENT "Generating documented examples")

SET(DOXYGEN_LAYOUT ${CMAKE_CURRENT_SOURCE_DIR}/DoxygenLayout.xml)
SET(DOXYGEN_IMAGES ${CMAKE_CURRENT_SOURCE_DIR})
SET(MARKDOWN_DOCS ${CMAKE_CURRENT_SOURCE_DIR}/md)
SET(DOCUMENTED_EXAMPLES ${CMAKE_SOURCE_DIR}/examples/documented)

MACRO(GENERATE_DOXYGEN_HTML CONFIG PAGES HTML_OUTPUT_DIR EXAMPLES_SCRIPT)
	SET(DOXYGEN_PAGES "${CMAKE_CURRENT_SOURCE_DIR}/${PAGES}")
	SET(DOXYGEN_GENERATED_PAGES "${CMAKE_CURRENT_BINARY_DIR}/${PAGES}")
	SET(DOXYGEN_EXAMPLES_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/${EXAMPLES_SCRIPT}")
	SET(DOXYGEN_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${HTML_OUTPUT_DIR}/index.html")
	SET(DOXYGEN_HTML_OUTPUT_DIR ${HTML_OUTPUT_DIR})
	STRING(REGEX REPLACE "([a-zA-Z_]*).in" "${CMAKE_CURRENT_BINARY_DIR}/\\1" DOXYGEN_GENERATED_CONFIG "${CONFIG}")

	file(MAKE_DIRECTORY ${DOXYGEN_GENERATED_PAGES})

	configure_file("${CMAKE_CURRENT_SOURCE_DIR}/${CONFIG}"
		${DOXYGEN_GENERATED_CONFIG} @ONLY)

	add_custom_command(OUTPUT ${DOXYGEN_OUTPUT}
		COMMAND ${PYTHON_EXECUTABLE} ${DOXYGEN_EXAMPLES_SCRIPT}
			${DOXYGEN_GENERATED_PAGES} ${DOCUMENTED_EXAMPLES}
		COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_GENERATED_CONFIG}
		DEPENDS ${DOXYGEN_GENERATED_CONFIG}
		COMMENT "Generating ${DOXYGEN_HTML_OUTPUT_DIR} documentation"
	)
	LIST(APPEND DOC_DEPENDENCIES ${DOXYGEN_OUTPUT})
ENDMACRO()

GENERATE_DOXYGEN_HTML(Doxyfile_en.in pages html insert_examples.py)
GENERATE_DOXYGEN_HTML(Doxyfile_cn.in pages_cn html_cn insert_examples_cn.py)
add_custom_target(doc DEPENDS documented_examples ${DOC_DEPENDENCIES})
